package com.vanke.vms.management.dao.daoImpl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.vanke.vms.common.domain.User;
import com.vanke.vms.management.dao.ManageUserDao;

public class ManageUserDaoImpl extends HibernateDaoSupport implements ManageUserDao{
	public List<User> loadUser(){
		String hql = "from User as u where u.validate = 4";
		List<User> applyUserList = (List<User>) getHibernateTemplate().find(hql);
		return applyUserList;
	}
	
	public List<User> loadForgetPassUser(){
		String hql = "from User as u where u.validate = 5";
		List<User> applyUserList = (List<User>) getHibernateTemplate().find(hql);
		return applyUserList;
	}

	public List<User> loadAllUser(){
		String hql = "from User";
		List<User> applyUserList = (List<User>) getHibernateTemplate().find(hql);
		return applyUserList;
	}
	
	public User loadUserInfo(String userId){
		String hql = "from User as u where u.userId = " + userId;
		User user = ((List<User>) getHibernateTemplate().find(hql)).get(0);
		return user;
		
	}
	
	public void modifyUser(User user){
		getHibernateTemplate().update(user);
	}
	
	public void deleteUser(Integer userId){
		String sql = "delete from vms.user where userid = " + userId.toString();
		
		SessionFactory sessionfactory = getHibernateTemplate()
			.getSessionFactory();
		Session session = sessionfactory.openSession();
		session.createSQLQuery(sql).executeUpdate();
		session.close();
	}
	
	public void approveApply(Integer userId){
		String sql = "update vms.user as u set u.validate = 0 where u.userid = " + userId.toString();
		
		SessionFactory sessionfactory = getHibernateTemplate()
			.getSessionFactory();
		Session session = sessionfactory.openSession();
		session.createSQLQuery(sql).executeUpdate();
		session.close();
	}
	
	public void removePass(Integer userId){
		String sql = "update vms.user as u set u.validate = 0 where u.userid = " + userId.toString();
		
		SessionFactory sessionfactory = getHibernateTemplate()
			.getSessionFactory();
		Session session = sessionfactory.openSession();
		session.createSQLQuery(sql).executeUpdate();
		session.close();
	}
	
	public void rejectApply(Integer userId){
		String sql = "delete from vms.user where userid = " + userId.toString();
		SessionFactory sessionfactory = getHibernateTemplate()
			.getSessionFactory();
		Session session = sessionfactory.openSession();
		session.createSQLQuery(sql).executeUpdate();
		session.close();
	}
}
